/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui.view.settings.table;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import com.genia.toolbox.basics.editor.model.document.VariableModel;

/**
 * The variable table model.
 */
@SuppressWarnings("serial")
public class VariableTableModel
    extends AbstractTableModel
{

  /**
   * The variables.
   */
  private List<VariableModel> variables = null;

  /**
   * The column names.
   */
  private String[] columnNames = null;



  /**
   * Constructor.
   * 
   * @param columnNames
   *          the column names.
   */
  public VariableTableModel(String[] columnNames)
  {
    super();
    this.variables = new ArrayList<VariableModel>();
    this.columnNames = columnNames;
  }



  /**
   * Get the column name.
   * 
   * @param column
   *          The column index.
   * @return the column name.
   */
  @Override
  public String getColumnName(int column)
  {
    return this.columnNames[column];
  }



  /**
   * Get the number of columns.
   * 
   * @return the number of columns.
   */
  public int getColumnCount()
  {
    return this.columnNames.length;
  }



  /**
   * Get the number of rows.
   * 
   * @return the number of rows.
   */
  public int getRowCount()
  {
    return this.variables.size();
  }



  /**
   * Get the value at the row index.
   * 
   * @param rowIndex
   *          the row index.
   * @return the value.
   */
  public VariableModel getValueAt(int rowIndex)
  {
    return this.variables.get(rowIndex);
  }



  /**
   * Get the value at the row index and the column index.
   * 
   * @param rowIndex
   *          the row index.
   * @param columnIndex
   *          the column index.
   * @return the value.
   */
  public Object getValueAt(int rowIndex, int columnIndex)
  {
    VariableModel variable = this.variables.get(rowIndex);
    Object displayedData = null;
    switch (columnIndex) {
      case 0:
        displayedData = variable.getName();
        break;
      case 1:
        displayedData = variable.getValue();
        break;
    }
    return displayedData;
  }



  /**
   * Set the value of the cell.
   * 
   * @param value
   *          the value.
   * @param rowIndex
   *          the row index.
   * @param columnIndex
   *          the column index.
   */
  @Override
  public void setValueAt(Object value, int rowIndex, int columnIndex)
  {
    VariableModel variable = this.variables.get(rowIndex);
    switch (columnIndex) {
      case 0:
        variable.setName((String) value);
        break;
      case 1:
        variable.setValue((String) value);
        break;
    }
    this.fireTableDataChanged();
  }



  /**
   * Return whether the cell is editable.
   * 
   * @param rowIndex
   *          the row index.
   * @param columnIndex
   *          the column index.
   * @return true if the cell is editable.
   */
  @Override
  public boolean isCellEditable(int rowIndex, int columnIndex)
  {
    boolean isCellEditable = false;
    if (columnIndex == 1) {
      isCellEditable = true;
    }
    return isCellEditable;
  }



  /**
   * Get the variables.
   * 
   * @return the variables.
   */
  public List<VariableModel> getVariables()
  {
    return this.variables;
  }



  /**
   * Set the variables.
   * 
   * @param variables
   *          the variables.
   */
  public void setVariables(List<VariableModel> variables)
  {
    this.variables = variables;
    this.fireTableDataChanged();
  }



  /**
   * Add a variable to the model.
   * 
   * @param variable
   *          the variable.
   */
  public void addVariable(VariableModel variable)
  {
    this.variables.add(variable);
    this.fireTableDataChanged();
  }



  /**
   * Remove a variable from the model.
   * 
   * @param variable
   *          the variable.
   */
  public void removeVariable(VariableModel variable)
  {
    this.variables.remove(variable);
    this.fireTableDataChanged();
  }



  /**
   * Clear the model.
   */
  public void clear()
  {
    this.variables.clear();
    this.fireTableDataChanged();
  }

}
